import { useUserStore } from "@/store/modules/user";

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}

/**
 * 解决两个数相乘精度丢失问题
 * @param {string} num1：被乘数  num2：乘数
 * @returns {Number}
 */
export function mul(num1: string, num2: string) {
  if (
    parseFloat(num1).toString() == "NaN" ||
    parseFloat(num2).toString() == "NaN"
  )
    return;
  let m = 0;
  const s1 = num1.toString();
  const s2 = num2.toString();
  try {
    m += s1.split(".")[1].length;
  } catch (e) {}
  try {
    m += s2.split(".")[1].length;
  } catch (e) {}
  return (
    (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) /
    Math.pow(10, m)
  );
}

/**
 * 单个权限验证
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
 */
export function auth(value: string): boolean {
  const stores = useUserStore();
  return stores.user.permissions.some((v: any) => v === value);
}

/**
 * 时间选择  - 禁止超过当前时间
 * @param time
 * @returns 返回 true 偶然 false，反之则反
 */
export function disableTime(time: Record<string, any>) {
  const nowT = new Date().getTime();
  return time.getTime() > nowT;
}
